package com.wanpinghui.wphlibrary.photovideo.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.yunshl.yunshllibrary.utils.LogUtils;

import org.xutils.common.util.DensityUtil;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2016/12/21
 * 版本:1.0
 ***/

public class RecordProgressBar extends View {
    private static final String TAG = "RecordProgressBar";
    private Paint mPaint;
    private RectF mRectF;
    private int maxProcess = 100;
    private int currentProcess;

    public RecordProgressBar(Context context) {
        this(context, null);
    }

    public RecordProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRectF = new RectF();
        mPaint = new Paint();
    }

    public int getProcess() {
        return currentProcess;
    }

    public void setProcess(int currentProcess) {
        this.currentProcess = currentProcess;
        this.invalidate();
    }

    public int getMaxProcess() {
        return maxProcess;
    }

    public void setMaxProcess(int maxProcess) {
        this.maxProcess = maxProcess;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();
        mPaint.reset();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#fed000"));
        int strokeWidth = DensityUtil.dip2px(3);
        if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
            canvas.drawOval(0, 0, (float) width, (float) height, mPaint);
        } else {
            canvas.drawCircle(width / 2, height / 2, width / 2, mPaint);
        }
        mRectF.left = strokeWidth / 2; // 左上角x
        mRectF.top = strokeWidth / 2; // 左上角y
        mRectF.right = width - (strokeWidth / 2); // 左下角x
        mRectF.bottom = height - (strokeWidth / 2); // 右下角y
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.parseColor("#FFFFFF"));
        LogUtils.w(TAG, "process " + currentProcess);
//        Path path = new Path();
//        path.moveTo(width/2f, 0);
//        path.arcTo(mRectF, -90, 360f*((float) currentProcess/maxProcess));
////path.addArc(new RectF(rect), 0, 90);
//        canvas.drawPath(path, mPaint);
        canvas.drawArc(mRectF, -90, 360f * ((float) currentProcess / maxProcess), false, mPaint);
    }
}
